package com.codeoss.framework.data.store;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.parser.ITableParser;
import com.codeoss.framework.util.TableBuilder;
import com.codeoss.module.ir.service.IIrModelService;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class TableStore implements ITableStore {
	private static final Logger logger = LoggerFactory.getLogger(TableStore.class);
	@Autowired
	private IIrModelService irModelService;
	@Autowired
	private ITableParser tableParser;
	private Map<String, Table> tableCache = new HashMap<String, Table>();
	@Autowired
	private ObjectMapper objectMapper;
	@Override
	public void put(String tableName, Table table) {
		// TODO Auto-generated method stub
		tableCache.put(tableName, table);
	}

	@Override
	public Table get(String tableName) {
		// TODO Auto-generated method stub
		Table table = tableCache.get(tableName);
		if (table == null) {
			table = init(tableName);
			if (table == null) {
				logger.warn("没有找到对应的表名" + tableName);
			}
			tableCache.put(tableName, table);
		}
		return table;
	}

	@Override
	public Table remove(String tableName) {
		// TODO Auto-generated method stub
		return tableCache.remove(tableName);
	}

	public Table init(String tableName) {
		return TableBuilder.build(tableName, objectMapper,tableParser, irModelService);
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		tableCache.clear();
	}
}
